<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" :close-on-click-modal='false' class="bonus">
    <el-row>
      <el-form :model="formData" ref="formRef" :label-width="labelWidth"  :rules="rules" v-loading="formLoading" :close-on-click-modal="false">
        <el-col :span="24">
            <el-form-item label="原方案名称" prop="oldComputeName">
              <el-input v-model="formData.oldComputeName" disabled autocomplete="off" placeholder="请输入原方案名称" clearable></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="新方案名称" prop="computeName">
              <el-input v-model="formData.computeName" autocomplete="off" placeholder="请输入新方案名称" clearable></el-input>
            </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel(false)">取消</el-button>
      <el-button type="primary"  @click="handleSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {copyScheme}  from '@/api/bonus/common' ;
export default {
  name: "copyScheme",
  props:{

  },
  data(){
    return{
      type:'add',
      formLoading:false,
      title:'复制方案',
      width:'30%',
      visible: false,
      labelWidth:'140px',
      formData: {oldComputeName:'',computeName:''},
      rules: {
        oldComputeName: [{ required: true, message: '请输入原方案名称', trigger: 'blur' },],
        computeName: [{ required: true, message: '请输入原方案名称', trigger: 'blur' },],
      },
    }
  },
  methods:{
    init(type,data){
      this.type = type ; //type => jg | ry
      this.$set(this.formData,'oldComputeName',data[0].computeName)
      this.$set(this.formData,'id',data[0].id)
      this.visible = true ;
    },

    handleSubmit(){
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.formLoading = true ;
          copyScheme(Object.assign(this.formData,{type:this.type})).then(res=>{
            this.formLoading = false ;
            this.handleCancel(true) ;
          }).catch(()=>{
            this.formLoading = false ;
          })
        } else {
          return false;
        }
      });
    },
    handleCancel(refresh){
      this.visible = false ;
      this.formData =  {oldComputeName:'',computeName:'',id:''};
      if (refresh){
        this.$emit('refresh')
      }
    },
  }
}
</script>

<style scoped>

</style>
